package com.lamatek.tags.google;

import java.io.Serializable;
import java.util.Vector;

import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;

/**
 * GoogleMapPolylineTag
 * 
 * This class represents a &lt;googlemaps;polyline> tag. Developers should not subclass or override this
 * class or it's methods.
 * 
 * @author Tom Cole
 * @version 0.40
 */
public class GoogleMapPolylineTag extends TagSupport implements Serializable, GoogleMapEventListener {
    
    private String id = null;
    private String pointlist = null;
    private String color = "#ff0000";
    private int weight = 2;
    private float opacity = 0.50f;
    Vector points = null; 
    Vector events = null;
    /**
     * Overrides doStartTag() in TagSupport. Developers should not override this method. 
     * If they do, they must call super.doStartTag() to ensure the tag get added to the
     * tree properly.
     */
    public int doStartTag() {
        Tag tag = this;
        while (tag.getParent() != null) {
            if (tag.getParent() instanceof GoogleMapTag) {
                ((GoogleMapTag) tag.getParent()).addPolyline(this);
                return SKIP_BODY;
            }
            tag = tag.getParent();
        }
        return SKIP_BODY;
    }
    /**
     * Sets the color used to draw this overlay. Default is #ff0000.
     * 
     * @param color The color code in hexadecimal format.
     */
    public void setColor(String color) {
        this.color = color;
    }
    /**
     * Sets the 0-1 based opacity of the line. 0 is invisible, 1 is fully opaque.
     * 
     * @param opacity A float from 0-1.
     */
    public void setOpacity(float opacity) {
        this.opacity = opacity;
    }
    /**
     * Sets the weight (width) of the line in pixels.
     * 
     * @param weight An int specifying line width in pixels.
     */
    public void setWeight(int weight) {
        this.weight = weight;
    }
    /**
     * Returns the number of points that make up this line or 0 if none are specified.
     * 
     * @return Number of points in the line, or 0.
     */
    public int pointCount() {
        if (points == null)
            return 0;
        else 
            return points.size();
    }
    /**
     * Returns the point located at the given iteration. Points are stored in order
     * to ensure the line is consistent between renderings.
     * 
     * @param int The iteration to lookup
     * @return The point's id
     */
    public String getPoint(int p) {
        if (points == null)
            return null;
        else 
            return (String) points.elementAt(p);
    }
    /**
     * Returns this overlays unique id.
     * 
     * @return The unique id of this overlay.
     */
    public String getId() {
        return id;
    }
    /**
     * Set the unique id for this polyline.
     * 
     * @param id A unique identifier for this polyline.
     */
    public void setId(String id) {
        this.id = id;
    }
    /**
     * Returns a comma separated list of point ids that generate this line.
     * 
     * @return A comma separated list of point ids, in order. 
     */
    public String getPointlist() {
        return pointlist;
    }
    /**
     * Sets the list of point ids that will makeup this line. The point
     * ids should be specified in a comma separated list, in the order
     * the line is to render.<p>
     * For example: point1, point3, point5, point7
     * 
     * @param pointlist A comma separated list of point names, in order.
     */
    public void setPointlist(String pointlist) {
        this.pointlist = pointlist;
        points = new Vector();
        String[] p = pointlist.split(",");
        for (int i = 0; i < p.length; i++) {
            points.add(p[i].trim());
        }
    }
    /**
     * Returns the color used to draw this line, in hexadecimal format.
     * 
     * @return A hexadecimal String representing a color.
     */
    public String getColor() {
        return color;
    }
    /**
     * Returns the current opacity level (0-1) of the line.
     * 
     * @return A float indicating opacity level.
     */
    public float getOpacity() {
        return opacity;
    }
    /**
     * Returns the current width (in pixels) if the line.
     * 
     * @return An int representing line width (in pixels).
     */
    public int getWeight() {
        return weight;
    }
    /**
     * Adds an event listener to this overlay.
     * 
     * @param event A GoogleMapEventTag
     */
    public void addEvent(GoogleMapEventTag event) {
        if (events == null)
            events = new Vector();
        events.add(event);
    }
    /**
     * Returns the list of currently registered event listeners
     * for this overlay.
     * 
     * @return A Vector of GoogleMapEventTags
     */
    public Vector getEvents() {
        return events;
    }
}
